package com.huasoft.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.huasoft.domain.Department;
import com.huasoft.util.JDBConnection;

public class DepartmentDao {
	public List<Department> findAll() {
		String sql = "select * from department";
		Connection con = null;
		Statement st = null;
		List<Department> departments = new ArrayList<Department>();
		ResultSet rs = null;
		try {
			con = JDBConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				Department department = new Department();
				department.setId(rs.getInt(1));
				department.setName(rs.getString(2));
				departments.add(department);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				JDBConnection.closeConnection(con, st, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return departments;

	}
}
